﻿using System;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using System.IO;

namespace WindowsPhoneAppHost
{
    public partial class Form : PhoneApplicationPage
    {
        public Form()
        {
            InitializeComponent();
//            DoHttpWebRequest();
            test();
        }

        private void test()
        {
            for (int i = 0; i < 5; i++)
            {
                listBox.Items.Add(new ListSeperator());
                listBox.Items.Add(new ListSeperator());
                listBox.Items.Add(new ListSeperator());
                for (int j = 0; j < 5; j++)
                {
                    FormListBoxItem formListBoxItem = new FormListBoxItem();
                    formListBoxItem.Title = "报销人";
                    formListBoxItem.Value = "XSX";
                    listBox.Items.Add(formListBoxItem);
                    listBox.Items.Add(new ListSeperator());
                }
            }

            //richtex
        }
        //*
        private void DoHttpWebRequest()
        {
            string url = "http://localhost/MobileOA/Form.js";
            WebRequest request = WebRequest.Create(url); //创建WebRequest类
            IAsyncResult result = request.BeginGetResponse(ResponseCallback, request); //返回异步操作的状态
        }

        private void ResponseCallback(IAsyncResult result)
        {
            var request = (HttpWebRequest)result.AsyncState; //获取异步操作返回的的信息
            WebResponse response = request.EndGetResponse(result); //结束对 Internet 资源的异步请求
            using (Stream stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string contents = reader.ReadToEnd();
                //Dispatcher.BeginInvoke(delegate() { this.textBlock1.Text = contents; });
                Dispatcher.BeginInvoke(delegate { ParseJson(contents); });
            }
        }

        private void ParseJson(string jsonString)
        {
            Debug.WriteLine(jsonString);
            JArray jArray = JArray.Parse(jsonString);
            Debug.WriteLine(jArray.Count.ToString());

            for (int i = 0; i < jArray.Count; i++)
            {
                var list = (JArray)jArray[i];
                if (0 != i)
                {
                    listBox.Items.Add(new ListSeperator());
                    listBox.Items.Add(new ListSeperator());
                    listBox.Items.Add(new ListSeperator());
                }
                listBox.Items.Add(new ListSeperator());
                for (int j = 0; j < list.Count; j++)
                {
                    var listobj = (JObject)list[i];
                    FormListBoxItem formListBoxItem = new FormListBoxItem();
                    formListBoxItem.Title = Common.getJsonString(listobj, "title");
                    formListBoxItem.Value = Common.getJsonString(listobj, "value");
                    listBox.Items.Add(formListBoxItem);
                    listBox.Items.Add(new ListSeperator());
                }
            }
        }
        //*/
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            //这里可以执行弹出对话框操作
            e.Cancel = true;
            MessageBox.Show("返回键不再返回");
        }
    }
}